﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WordCloudInterface
{
    public partial class Toolbox : Form
    {
        private Interface parentForm;

        public Toolbox(Interface form)
        {
            this.parentForm = form;

            InitializeComponent();

            // Change window upper-left icon
            this.Icon = Interface.GetProgramIcon();

            // Configue trackbars
            this.ConfigureCollisionSensitivityTrackbar();
            this.ConfigureWordSizeSensitivityTrackbar();
            this.ConfigureFontFamilyDropdownList();
            this.ConfigureMinFontSizeSensitivityTrackbar();
            this.ConfigureMaxFontSizeSensitivityTrackbar();
            this.ConfigureColorsSensitivityTrackbar();
            this.ConfigureBackgroundColorDropdownList();
            this.ConfigureLimitByOccurenceTrackbar();

            // Set event handlers
            this.trackBar1.ValueChanged += CollisionSensitivity_ValueChanged;
            this.trackBar3.ValueChanged += WordSizeSensitivity_ValueChanged;
            this.comboBox1.SelectedValueChanged += FontFamily_SelectedValueChanged;
            this.trackBar4.ValueChanged += MinFontSizeSensitivity_ValueChanged;
            this.trackBar5.ValueChanged += MaxFontSizeSensitivity_ValueChanged;
            this.checkBox1.CheckedChanged += UseAllColors_Click;
            this.checkBox2.CheckedChanged += RandomizeColors_Click;
            this.trackBar2.ValueChanged += ColorsSensitivity_ValueChanged;
            this.comboBox2.SelectedValueChanged += BackgroundColor_SelectedValueChanged;
            this.trackBar6.ValueChanged += LimitByOccurence_ValueChanged;
            this.checkBox3.CheckedChanged += StackImports_Click;

            // Create the ToolTip and associate with the Form container.
            ToolTip toolTip1 = new ToolTip();

            // Set up the delays for the ToolTip.
            toolTip1.AutoPopDelay = 5000;
            toolTip1.InitialDelay = 1000;
            toolTip1.ReshowDelay = 500;
            // Force the ToolTip text to be displayed whether or not the form is active.
            toolTip1.ShowAlways = true;

            toolTip1.SetToolTip(this.checkBox3, "The word cloud generated with each import will be superimposed with the following.");

            this.FormClosing += Toolbox_FormClosing;
        }

        private void Toolbox_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.parentForm.CloseToolBox();
        }

        private void ConfigureCollisionSensitivityTrackbar()
        {
            this.trackBar1.Minimum = -20;
            this.trackBar1.Maximum = 20;
            this.trackBar1.Value = 0;
        }

        private void ConfigureWordSizeSensitivityTrackbar()
        {
            this.trackBar3.Minimum = -8;
            this.trackBar3.Maximum = 40;
            this.trackBar3.Value = 0;
        }

        private void ConfigureFontFamilyDropdownList()
        {
            InstalledFontCollection installedFontCollection = new InstalledFontCollection();
            FontFamily[] fontFamilies = installedFontCollection.Families;
            int count = fontFamilies.Length;
            for (int j = 0; j < count; ++j)
            {
                this.comboBox1.Items.Add(new NameValue(fontFamilies[j].Name, fontFamilies[j].Name));
            }
        }

        private void ConfigureMinFontSizeSensitivityTrackbar()
        {
            this.trackBar4.Minimum = 1;
            this.trackBar4.Maximum = 60;
            this.trackBar4.Value = 6;
        }

        private void ConfigureMaxFontSizeSensitivityTrackbar()
        {
            this.trackBar5.Minimum = 20;
            this.trackBar5.Maximum = 80;
            this.trackBar5.Value = 68;
        }

        private void ConfigureColorsSensitivityTrackbar(int length = -1)
        {
            this.trackBar2.Minimum = 0;
            if (length > 0)
                this.trackBar2.Maximum = length;
            else
                this.trackBar2.Maximum = Interface.GetColorPalette(this.checkBox1.Checked).Length;
        }

        private void ConfigureBackgroundColorDropdownList()
        {
            string[] colors = Enum.GetNames(typeof(System.Drawing.KnownColor));
            foreach (string colorName in colors)
            {
                this.comboBox2.Items.Add(new NameValue(colorName, colorName));
            }
        }

        private void ConfigureLimitByOccurenceTrackbar()
        {
            this.trackBar6.Minimum = 0;
            this.trackBar6.Maximum = 20;
            this.trackBar6.Value = 0;
        }

        private void CollisionSensitivity_ValueChanged(object sender, EventArgs e)
        {
            this.parentForm.GetWordCloud().CollisionSensitivity = Convert.ToDouble(((TrackBar)sender).Value);
        }

        private void ColorsSensitivity_ValueChanged(object sender, EventArgs e)
        {
            this.parentForm.GetWordCloud().ColorsSensitivity = Convert.ToInt32(((TrackBar)sender).Value);
        }

        private void WordSizeSensitivity_ValueChanged(object sender, EventArgs e)
        {
            this.parentForm.GetWordCloud().FontSizeSensitivity = Convert.ToInt32(((TrackBar)sender).Value);
        }

        private void MinFontSizeSensitivity_ValueChanged(object sender, EventArgs e)
        {
            this.parentForm.GetWordCloud().MinFontSize = Convert.ToInt32(((TrackBar)sender).Value);
        }

        private void MaxFontSizeSensitivity_ValueChanged(object sender, EventArgs e)
        {
            this.parentForm.GetWordCloud().MaxFontSize = Convert.ToInt32(((TrackBar)sender).Value);
        }

        private void FontFamily_SelectedValueChanged(object sender, System.EventArgs e)
        {
            try
            {
                string fontFamily = ((NameValue)((ComboBox)sender).SelectedItem).DataValue;
                this.parentForm.GetWordCloud().Font = new System.Drawing.Font(fontFamily, 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            }
            catch
            {
                this.parentForm.GetWordCloud().Font = new System.Drawing.Font("Times New Roman", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            }
        }

        private void BackgroundColor_SelectedValueChanged(object sender, EventArgs e)
        {
            try
            {
                string backgroundColor = ((NameValue)((ComboBox)sender).SelectedItem).DataValue;
                this.parentForm.GetWordCloud().BackColor = Color.FromName(backgroundColor);
            }
            catch
            {
                this.parentForm.GetWordCloud().BackColor = Color.White;
            }
        }

        private void RandomizeColors_Click(object sender, EventArgs e)
        {
            this.parentForm.GetWordCloud().RandomizeColors = ((CheckBox)sender).Checked;
        }

        private void UseAllColors_Click(object sender, EventArgs e)
        {
            Color[] colors = Interface.GetColorPalette(((CheckBox)sender).Checked);

            this.ConfigureColorsSensitivityTrackbar(colors.Length);
            this.parentForm.GetWordCloud().Palette = colors;
        }

        private void LimitByOccurence_ValueChanged(object sender, EventArgs e)
        {
            this.parentForm.OccurenceLimit = Convert.ToInt32(((TrackBar)sender).Value);
        }

        private void StackImports_Click(object sender, EventArgs e)
        {
            this.parentForm.StackImports = ((CheckBox)sender).Checked;
        }
    }
}
